﻿namespace ChumChase.Twitter
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows;
    using DefaultFeeds;
    using Extensions;
    using LinqToTwitter;
    using Model;
    using Services;
    using Views;

    public class TwitterProvider : IFeedProvider
    {
        private readonly IList<IFeedIdentity> _defaultFeeds = new List<IFeedIdentity>();
        private readonly IList<DataTemplate> _templates = new List<DataTemplate>();

        private TwitterSettings _settings = new TwitterSettings();
        private TwitterContext _twitterCtx;

        public TwitterProvider()
        {
            _templates.AddDataTemplateFor<FeedEntry, Tweet>();
            _templates.AddDataTemplateFor<TwitterSettings, SettingsTemplate>();

            _defaultFeeds.Add(new HomeFeed());
            _defaultFeeds.Add(new Replies());
            _defaultFeeds.Add(new DirectMessages());
        }

        public void ApplySettings()
        {
            _twitterCtx = new TwitterContext(_settings.Username, _settings.Password,
                                             "http://www.twitter.com/",
                                             "http://search.twitter.com/");
        }

        public string DisplayName
        {
            get { return "Twitter"; }
        }

        public IEnumerable<DataTemplate> DataTemplates
        {
            get { return _templates; }
        }

        public IFeedProviderSettings Settings
        {
            get { return _settings; }
        }

        public IEnumerable<IFeedEntry> GetEntriesFor(string feedId)
        {
            if (_twitterCtx == null) return new List<IFeedEntry>();

            IQueryable<Status> tweets = from tweet in _twitterCtx.Status
                                        where tweet.Type == StatusType.Friends
                                        select tweet;

            //HACK: Linq2Twitter seems to have a bug that prevents me from doing this all in one query

            IEnumerable<IFeedEntry> entries = from tweet in tweets.ToList()
                                              select new FeedEntry
                                                         {
                                                             Id = tweet.ID,
                                                             Title = tweet.Text,
                                                             CreatedOn = tweet.CreatedAt,
                                                             Service = "Twitter",
                                                             ProfileImageUrl = tweet.User.ProfileImageUrl
                                                         } as IFeedEntry;

            return entries;
        }

        public void LoadSettings(ILocalStorage storage)
        {
            _settings = storage.Load<TwitterSettings>(_settings.DetermineFileName()) ?? new TwitterSettings();
        }

        public void SaveSettings(ILocalStorage storage)
        {
            storage.Save(_settings, _settings.DetermineFileName());
        }

        public IEnumerable<IFeedIdentity> DefaultFeeds
        {
            get { return _defaultFeeds; }
        }
    }
}